package com.lzl.edu.controller.stu;

import com.lzl.edu.pojo.Student;
import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * <h3>lzl_ws</h3>
 * <p>Student的controller</p>
 *
 * @author : 黎钟龙
 * QQ:1661548820
 * Mail：1661548820@qq.com
 * @date : 2022-03-24 10:13
 **/
@Controller
@RequestMapping("/stu")
@Slf4j
public class StudentController {

    /**
     * 初始化stu对象返回到initial页面上
     * 测试分支语句
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String initialStu(Model model){
        log.info("到达stu的修改信息页面");
        Student student = new Student();
        student.setStuId(1).setStuName("小州").setStuAge(22).setStuSexNo(1);
//        设置日期格式
        Calendar calendar= new GregorianCalendar();
        calendar.set(2000,11,22);
//        把日期放入到stu的Date类型中
        student.setStuBirthday(calendar.getTime());
        student.setStuSex("男");
        model.addAttribute("stu",student);
        log.info("stu初始化结束并返回到initial页面上");
        return "stu/initial";
    }


    @RequestMapping("/iterator")
    public String doIterator(Model model) throws ParseException {
//        创建集合
        List<Student> students = new ArrayList<>();
//        往集合中放入数据
        students.add(new Student(1,"小黎",22,null,"男",1));
        students.add(new Student(2,"小仪",21,null,"女",2));
        students.add(new Student(3,"小王",22,null,"男",1));
        students.add(new Student(4,"小李",22,null,"男",1));
        Student studentTemp = new Student(5, "小程", 22,
                Date.from(LocalDateTime.of(
                                2000,6,8,22,23,30,30)
                        .atZone(ZoneId.systemDefault()).toInstant())
                , "男", 1);
        students.add(studentTemp);
//        日期转化以下两行可以写为        Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("2000-10-22");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date birthday=format.parse("2000-10-22");
//        下一行可以写为        students.add(new Student(6,"小张",21,new SimpleDateFormat("yyyy-MM-dd").parse("2000-10-22"),"女",2);
        students.add(new Student(6,"小张",21,birthday,"女",2));
//        添加到model中
        model.addAttribute("students",students);
//        返回到iterator视图
        return "stu/iterator";
    }

    /**
     * 使用map集合遍历数据
     * @param model
     * @return
     */
    @RequestMapping("/iteratorbymap")
    public String iterationMap(Model model) throws ParseException {
//        mapper集合
        Map<String,Student> studentMap=new HashMap<>();
        studentMap.put("a",new Student(1,"小黎",22,null,"男",1));
        studentMap.put("b",new Student(2,"小仪",21,null,"女",2));
        studentMap.put("c",new Student(3,"小王",22,null,"男",1));
        studentMap.put("d",new Student(4,"小李",22,null,"男",1));
        studentMap.put("e",new Student(5, "小程", 22,
                Date.from(LocalDateTime.of(
                                2000,6,8,22,23,30,30)
                        .atZone(ZoneId.systemDefault()).toInstant())
                , "男", 1));
        studentMap.put("f",new Student(6,"小张",21,new SimpleDateFormat("yyyy-MM-dd").parse("2000-5-12"),"女",2));
        model.addAttribute("stuMap",studentMap);
        return "stu/iteratorMap";
    }


}
